using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Forms;
using yW5Lib;

namespace yWriter6
{
	public class clsPrintSceneIndexCards
	{
		public delegate void PrintProgressEventHandler(string ProgressString);

		[CompilerGenerated]
		[AccessedThroughProperty("PrintPvw")]
		private PrintPreviewDialog _PrintPvw;

		[CompilerGenerated]
		[AccessedThroughProperty("PrintDLG")]
		private PrintDialog _PrintDLG;

		[CompilerGenerated]
		[AccessedThroughProperty("PrintDoc")]
		private PrintDocument _PrintDoc;

		[CompilerGenerated]
		private PrintProgressEventHandler PrintProgressEvent;

		private int Row;

		private int Col;

		private int Pagenum;

		private int NumRows;

		private int NumCols;

		private int CardsPerPage;

		private clsProject cProject;

		private int ChapterIndex;

		private int SceneIndex;

		private int FirstChapter;

		private int LastChapter;

		private bool ShowUnused;

		private int Margin;

		private int TopMargin;

		private Pen P;

		private Font F;

		private string FontName;

		private float FontSize;

		private DateTime PrintDate;

		private int PageTop;

		private int PageWidth;

		private int PageHeight;

		private bool bPageSetup;

		[field: AccessedThroughProperty("PrintPvw")]
		protected virtual PrintPreviewDialog PrintPvw
		{
			get;
			[MethodImpl(MethodImplOptions.Synchronized)]
			set;
		}

		protected virtual PrintDialog PrintDLG
		{
			[CompilerGenerated]
			get
			{
				return _PrintDLG;
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			[CompilerGenerated]
			set
			{
				EventHandler value2 = PrintDLG_Disposed;
				EventHandler value3 = PrintDLG_HelpRequest;
				PrintDialog printDLG = _PrintDLG;
				if (printDLG != null)
				{
					printDLG.Disposed -= value2;
					printDLG.HelpRequest -= value3;
				}
				_PrintDLG = value;
				printDLG = _PrintDLG;
				if (printDLG != null)
				{
					printDLG.Disposed += value2;
					printDLG.HelpRequest += value3;
				}
			}
		}

		protected virtual PrintDocument PrintDoc
		{
			[CompilerGenerated]
			get
			{
				return _PrintDoc;
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			[CompilerGenerated]
			set
			{
				PrintEventHandler value2 = PrintDoc_BeginPrint;
				EventHandler value3 = PrintDoc_Disposed;
				PrintEventHandler value4 = PrintDoc_EndPrint;
				PrintPageEventHandler value5 = PrintDoc_PrintPage;
				QueryPageSettingsEventHandler value6 = PrintDoc_QueryPageSettings;
				PrintDocument printDoc = _PrintDoc;
				if (printDoc != null)
				{
					printDoc.BeginPrint -= value2;
					printDoc.Disposed -= value3;
					printDoc.EndPrint -= value4;
					printDoc.PrintPage -= value5;
					printDoc.QueryPageSettings -= value6;
				}
				_PrintDoc = value;
				printDoc = _PrintDoc;
				if (printDoc != null)
				{
					printDoc.BeginPrint += value2;
					printDoc.Disposed += value3;
					printDoc.EndPrint += value4;
					printDoc.PrintPage += value5;
					printDoc.QueryPageSettings += value6;
				}
			}
		}

		public event PrintProgressEventHandler PrintProgress
		{
			[CompilerGenerated]
			add
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Combine(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Remove(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
		}

		public clsPrintSceneIndexCards()
		{
			PrintPvw = new PrintPreviewDialog();
			PrintDLG = new PrintDialog();
			PrintDoc = new PrintDocument();
			Row = 0;
			Col = 0;
			Pagenum = 0;
			NumRows = 2;
			NumCols = 3;
			CardsPerPage = NumRows * NumCols;
			Margin = 10;
			TopMargin = 30;
			P = Pens.Black;
			PageTop = 0;
			PageWidth = 0;
			PageHeight = 0;
			bPageSetup = false;
		}

		public void PrintIndexCards(clsProject ProjectClass, bool WithPreview)
		{
			cProject = ProjectClass;
			if (cProject.ChapterCount != 0)
			{
				clsCollection<int> clsCollection = new clsCollection<int>();
				frmPickChapters frmPickChapters = new frmPickChapters(ref cProject);
				frmPickChapters.EnablePrinterSelect();
				frmPickChapters.ShowDialog();
				if (!frmPickChapters.Cancelled)
				{
					clsCollection = frmPickChapters.PickedIDs;
					FirstChapter = cProject.Chapters.IndexFromID(clsCollection[1]);
					LastChapter = cProject.Chapters.IndexFromID(clsCollection[clsCollection.Count()]);
					ShowUnused = frmPickChapters.WithUnused;
					try
					{
						frmIndexCards frmIndexCards = new frmIndexCards();
						if (frmIndexCards.ShowDialog() != DialogResult.OK)
						{
							return;
						}
						NumRows = Convert.ToInt32(frmIndexCards.NUDDown.Value);
						NumCols = Convert.ToInt32(frmIndexCards.NUDAcross.Value);
						Margin = Convert.ToInt32(frmIndexCards.NUDMargin.Value);
						FontName = frmIndexCards.cmbFont.Text;
						FontSize = Conversions.ToSingle(frmIndexCards.cmbFontSize.Text);
						SetupFont();
						ResetVariables();
					}
					catch (Exception ex)
					{
						ProjectData.SetProjectError(ex);
						Exception ex2 = ex;
						clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
						ProjectData.ClearProjectError();
					}
					try
					{
						if (WithPreview)
						{
							PrintDoc.PrinterSettings = frmPickChapters.PrintSettings;
							PrintPvw.Document = PrintDoc;
							PrintPvw.ShowDialog();
						}
						else
						{
							PrintDLG.PrinterSettings = frmPickChapters.PrintSettings;
							if (PrintDLG.ShowDialog() == DialogResult.OK)
							{
								PrintProgressEvent?.Invoke("Printing index cards");
								PrintDoc.PrinterSettings = PrintDLG.PrinterSettings;
								PrintDoc.Print();
							}
						}
					}
					catch (Exception ex3)
					{
						ProjectData.SetProjectError(ex3);
						Exception ex4 = ex3;
						clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
						ProjectData.ClearProjectError();
					}
				}
			}
		}

		private void PrintDoc_BeginPrint(object sender, PrintEventArgs e)
		{
		}

		private void PrintDoc_Disposed(object sender, EventArgs e)
		{
		}

		private void PrintDoc_EndPrint(object sender, PrintEventArgs e)
		{
		}

		private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
		{
			PrintNextPage(e);
		}

		private void PrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
		{
		}

		private void PrintDLG_Disposed(object sender, EventArgs e)
		{
		}

		private void PrintDLG_HelpRequest(object sender, EventArgs e)
		{
		}

		private void PrintNextPage(PrintPageEventArgs e)
		{
			try
			{
				if (!bPageSetup)
				{
					PageHeight = (int)Math.Round(e.PageSettings.PrintableArea.Height);
					PageWidth = (int)Math.Round(e.PageSettings.PrintableArea.Width);
					PageTop = (int)Math.Round(e.PageSettings.PrintableArea.Top);
					bPageSetup = true;
				}
				float num;
				float num2;
				if (e.PageSettings.Landscape)
				{
					num = PageHeight;
					num2 = PageWidth - TopMargin - Margin * 2;
				}
				else
				{
					num = PageWidth;
					num2 = PageHeight - TopMargin - Margin * 2;
				}
				float num3 = num / (float)NumCols;
				float num4 = num2 / (float)NumRows;
				bool flag = false;
				num3 -= (float)Margin;
				num4 -= (float)Margin;
				Pagenum++;
				int num5 = 0;
				PrintProgressEvent?.Invoke("Printing page " + Conversions.ToString(Pagenum));
				Col = 1;
				Row = 1;
				int chapterIndex = ChapterIndex;
				int lastChapter = LastChapter;
				int i;
				for (i = chapterIndex; i <= lastChapter; i++)
				{
					clsChapter clsChapter = cProject.Chapters.ChapterByIndex(i);
					if (clsChapter.SceneCount > 0)
					{
						if (SceneIndex > clsChapter.SceneCount)
						{
							SceneIndex = 1;
						}
						else
						{
							if (!clsChapter.Unused | (clsChapter.Type == ChapterTypes.ChapterNormal) | ShowUnused)
							{
								int sceneIndex = SceneIndex;
								int sceneCount = clsChapter.SceneCount;
								int j;
								for (j = sceneIndex; j <= sceneCount; j++)
								{
									clsScene clsScene = cProject.get_SceneByID(clsChapter.get_SceneIDByIndex(j));
									if (!clsScene.Unused | ShowUnused)
									{
										if (!flag)
										{
											e.Graphics.DrawString(cProject.Title + " by " + cProject.AuthorName + ". Page " + Conversions.ToString(Pagenum) + " (Printed " + Conversions.ToString(PrintDate) + ")", F, Brushes.Black, Margin, e.PageSettings.PrintableArea.Top);
											flag = true;
										}
										PrintCard(e, num3, num4, clsChapter, clsScene);
										num5++;
									}
									if (num5 >= CardsPerPage)
									{
										break;
									}
								}
								if (j >= clsChapter.SceneCount)
								{
									SceneIndex = 1;
								}
								else
								{
									SceneIndex = j + 1;
								}
							}
							if (num5 >= CardsPerPage)
							{
								break;
							}
						}
					}
				}
				e.Graphics.Dispose();
				ChapterIndex = i;
				if (ChapterIndex <= LastChapter)
				{
					e.HasMorePages = true;
				}
				else
				{
					e.HasMorePages = false;
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		private void PrintCard(PrintPageEventArgs e, float CardW, float CardH, clsChapter Chapter, clsScene Scene)
		{
			string text = "";
			try
			{
				Graphics graphics = e.Graphics;
				int value = Conversions.ToInteger(Conversions.ToString(Scene.Wordcount));
				string text2 = Scene.Title.Trim();
				string text3 = Scene.DescText.Trim();
				Chapter.Title.Trim();
				if (Operators.CompareString(text3, "", TextCompare: false) == 0)
				{
					text3 = "No summary";
				}
				if (Col > NumCols)
				{
					Col = 1;
					Row++;
				}
				if (Row > NumRows)
				{
					Col = 1;
					Row = 1;
				}
				float num = (float)(Col - 1) * (CardW + (float)Margin) + (float)Margin;
				float num2 = (float)PageTop + (float)(Row - 1) * (CardH + (float)Margin) + (float)TopMargin;
				graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
				graphics.DrawRectangle(P, num, num2, CardW, CardH);
				text = Chapter.Title + "." + Conversions.ToString(Chapter.SceneIndex(Scene.ID)) + ") ";
				text = text + text2.Trim() + " ";
				text = text + "(" + Conversions.ToString(value) + " words)\r\n---\r\n";
				text += text3;
				graphics.MeasureString(text, F);
				_ = (int)Math.Round(CardH / (float)F.Height);
				RectangleF layoutRectangle = new RectangleF(num + 3f, num2 + 3f, CardW - 6f, CardH - 6f);
				StringFormat stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.LineLimit);
				graphics.MeasureString(text, F, new SizeF(CardW - 3f, CardH - 3f), stringFormat, out int _, out int _);
				graphics.DrawString(text, F, Brushes.Black, layoutRectangle, stringFormat);
				Col++;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		private void ResetVariables()
		{
			try
			{
				CardsPerPage = NumRows * NumCols;
				Pagenum = 0;
				ChapterIndex = FirstChapter;
				SceneIndex = 1;
				bPageSetup = false;
				TopMargin = (int)Math.Round(FontSize * 2f);
				if (TopMargin < 20)
				{
					TopMargin = 20;
				}
				PrintDate = DateAndTime.Now;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		private void SetupFont()
		{
			try
			{
				F = new Font(FontName, FontSize);
				TopMargin = (int)Math.Round(FontSize * 2f);
				if (TopMargin < 20)
				{
					TopMargin = 20;
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}
	}
}
